package springlearn;

import java.io.Reader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import javax.sql.DataSource;

import com.ibatis.common.jdbc.SimpleDataSource;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class TestDAO {
	public SqlMapClient getConnected(){
		try{
/*			String resource = "ibatislearn/SqlMapConfig.properties";
			Properties props = Resources.getResourceAsProperties (resource);
			DataSource dataSource = new SimpleDataSource(props );
			Connection conn = dataSource.getConnection();			
			conn.commit();
			conn.close();*/
			
			String resource2 = "sql-map-config.xml";
			Reader reader = Resources.getResourceAsReader(resource2);
			SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
			return sqlMap;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}

	}
	public void closeConn(SqlMapClient sqlMap){
		try{
			sqlMap.endTransaction();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void Test(){
		SqlMapClient sqlMap = this.getConnected();
		try{
			List l = sqlMap.queryForList("getAccessTypeById",""+9);
			System.out.println("over");
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			this.closeConn(sqlMap);
		}
		
	}

}
